package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.MallShopQueryDTO;
import com.cheer.entity.MallCategoryEntity;
import com.cheer.entity.MallShopEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cheer.vo.SimpleObjectVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商户表
 * 
 * @author cheer
 */
@Mapper
@Repository
public interface MallShopDao extends BaseMapper<MallShopEntity> {

    default IPage<MallShopEntity> selectPageByQueryCondition(MallShopQueryDTO shopQueryDTO) {
        QueryWrapper<MallShopEntity> queryWrapper = new QueryWrapper<>();

        String contactInfo = StringUtils.trimToNull(shopQueryDTO.getContactInfo());
        if (contactInfo != null) {
            queryWrapper.and(shopEntityQueryWrapper -> shopEntityQueryWrapper
                    .like("contact_person", contactInfo)
                    .or()
                    .eq("phone", contactInfo));
        }

        String name = StringUtils.trimToNull(shopQueryDTO.getName());
        if (name != null) {
            queryWrapper.like("name", name);
        }

        queryWrapper.orderByDesc("create_time");

        return selectPage(new Page<>(shopQueryDTO.getPageNum(), shopQueryDTO.getPageSize()), queryWrapper);
    }

    default MallShopEntity selectOneByPhone(String phone) {
        return selectOne(new QueryWrapper<MallShopEntity>()
                .eq("phone", phone)
                .last("limit 1"));
    }

    default List<MallShopEntity> selectName(){
        return selectList(new QueryWrapper<MallShopEntity>()
                .select("id", "name"));
    }

}
